// pages/goods/list/index.js
import { getGoodsListApi } from '../../../api/goods'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [], // 商品列表数据
    isFinish: false, // 判断数据是否加载完毕
    requestData: {
      page: 1,
      limit: 10,
      category1Id: '',
      category2Id: ''
    },
    total: '',
    isLoading: false
  },
  async getGoodsList() {
    this.data.isLoading = true
    const res = await getGoodsListApi(this.data.requestData)
    // console.log(res)
    this.setData({
      goodsList: [...this.data.goodsList, ...res.data.records],
      total: res.data.total,
      isLoading: false
    })
  },
  gotoBack() {
    wx.navigateBack()
  },
  onLoad(options) {
    // console.log(options)
    Object.assign(this.data.requestData, options)
    this.getGoodsList()
  },
  onPullDownRefresh() {
    // 将数据重置
    this.setData({
      goodsList: [],
      total: 0,
      isFinish: false,
      requestData: {
        ...this.data.requestData,
        page: 1
      }
    })
    // 使用最新参数发送请求
    this.getGoodsList()
    // 关闭下拉刷新效果
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    if (this.data.goodsList.length === this.data.total) {
      this.data.isFinish = true
      return
    }
    if (this.data.isLoading) {
      return
    }
    this.setData({
      'requestData.page': this.data.requestData.page + 1
    })
    this.getGoodsList()
  },
  onShareAppMessage() {
    return {
      title: '所有的怦然心动，都是你',
      path: '/pages/index/index',
      imageUrl: '../../../../../assets/images/love.jpg'
    }
  },

  onShareTimeline() {}
})
